package com.zhaojonhy.zhaopangwb.ui;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.exception.WeiboException;
import com.sina.weibo.sdk.net.RequestListener;
import com.sina.weibo.sdk.openapi.CommentsAPI;
import com.zhaojonhy.zhaopangwb.R;
import com.zhaojonhy.zhaopangwb.adapter.CommentsTomeAdapter;
import com.zhaojonhy.zhaopangwb.bean.CommentToMe;
import com.zhaojonhy.zhaopangwb.bean.Comments;
import com.zhaojonhy.zhaopangwb.utils.AccessTokenKeeper;
import com.zhaojonhy.zhaopangwb.utils.Constants;
import com.zhaojonhy.zhaopangwb.utils.TitleBuilder;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/10/31.
 */

public class CommentToMeActivity extends Activity implements View.OnClickListener{

    private static final String TAG = CommentToMeActivity.class.getSimpleName() ;

    private Oauth2AccessToken mAccessToken;
    private CommentsAPI mCommentsAPI ;
    private List<Comments> commentList = new ArrayList<Comments>();
    private PullToRefreshListView plvHome ;
    private View footView ;
    private CommentsTomeAdapter adapter ;
    private int curPage = 1 ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment_tome);

        mAccessToken = AccessTokenKeeper.readAccessToken(this) ;
        mCommentsAPI = new CommentsAPI(this, Constants.APP_KEY, mAccessToken) ;

        initListView() ;
        loadStatus( 1 ) ;
    }

    private void initListView () {
        new TitleBuilder(this)
                .setTitleText("所有评论")
                .setTitleBgRes(R.color.white)
                .setLeftImage(R.drawable.navigationbar_back_sel)
                .setLeftOnClickListener(this)
                .build();

        plvHome = (PullToRefreshListView)findViewById(R.id.lv_comment_tome) ;
        adapter = new CommentsTomeAdapter(this,commentList) ;
        Log.d(TAG,"adapter-->" + adapter.getCount()) ;
        plvHome.setAdapter(adapter); //新建一个空的adapter，在添加数据再notifyDataSetChanged listView

        plvHome.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ListView> refreshView) {
                loadStatus( 1 ) ; //加载了第一页
            }
        });

        //不需要手拉加载，判断当显示左后一个Item是加载下一页
        plvHome.setOnLastItemVisibleListener(new PullToRefreshBase.OnLastItemVisibleListener() {
            @Override
            public void onLastItemVisible() {
                loadStatus( curPage + 1 ) ;
            }
        });
        footView = View.inflate(this , R.layout.footview_loading , null) ;
    }

    /**
     * 获取微博内容
     */
    private void loadStatus(final int page) {
        mCommentsAPI.toME(0,0,50,page,0,0,new RequestListener(){

            @Override
            public void onComplete(String response) {
                Log.d(TAG,"response :" + response ) ;
                if (page == 1) {
                    commentList.clear(); //加载第一页时清空集合，确保数据是同一个，及下拉刷新
                }

                curPage = page ; //把加载页赋值给当前页
                Gson gson = new Gson() ;
                CommentToMe commentToMe = gson.fromJson(response, CommentToMe.class) ;
                addData(commentToMe) ;

            }

            @Override
            public void onWeiboException(WeiboException e) {

            }
        }) ;
    }

    private void addData(CommentToMe commentToMe) {

        Log.d(TAG,"comments-->" + commentToMe.getComments()) ;

        for (Comments comments : commentToMe.getComments()) {
            Log.d(TAG,"comments-->" + comments.getUser().getName()) ;
            //接口 List<E>containsboolean contains(Object o)如果列表包含指定的元素，则返回 true。
            // 更确切地讲，当且仅当列表包含满足 (o==null ? e==null : o.equals(e)) 的元素 e 时才返回 true。
            if (!commentList.contains(comments)) { //list.contains(Bean) ,防止数据的重复添加
                Log.d(TAG,"comments-->" + comments.getUser().getName()) ;
                commentList.add(comments); //数据添加到集合中
            }
        }
        Log.d(TAG,"commentLists-->" + commentList) ;
        //可能要开线程
        adapter.notifyDataSetChanged();//
        Log.d(TAG,"adapter-->" + "load:" +adapter.getCount()) ;

        plvHome.onRefreshComplete();

        if (curPage < commentToMe.getTotal_number()) { //判断是否继续加载 ,当前页小于总页数
            addFootView(plvHome, footView); //继续加载
        } else {
            removeFootView(plvHome, footView); //移除加载
        }
    }

    private void addFootView(PullToRefreshListView plvHome, View footView ) {
        ListView lv = plvHome.getRefreshableView() ;
        Log.d(TAG,"lv.getFooterViewsCount()-->" + lv.getFooterViewsCount()) ;
        if(lv.getFooterViewsCount() == 1) {//查看listView的源码此数据是返回listView的List的集合size
            lv.addFooterView(footView); //添加视图，只进行添加一次,当add前count==1 后 count==2
        }
    }

    private void removeFootView(PullToRefreshListView plvHome, View footView) {
        ListView lv = plvHome.getRefreshableView();
        if (lv.getFooterViewsCount() > 1) {
            lv.removeFooterView(footView);
        }
    }


    @Override
    public void onClick(View v) {

    }
}
